package com.education.wisdom.controller.management;

import com.education.common.config.BusinessException;
import com.education.common.config.CommonErrorCode;
import com.education.common.utils.ApiReturnObject;
import com.education.wisdom.pojo.Catalog;
import com.education.wisdom.pojo.CatalogFree;
import com.education.wisdom.pojo.Microlesson;
import com.education.wisdom.service.ICatalogFreeService;
import com.education.wisdom.service.IMicrolessonService;
import com.education.wisdom.service.impl.CatalogFreeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.Id;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/Manage/CatalogFree")
public class CatalogFreeController {

    @Autowired
    private ICatalogFreeService catalogFreeService;

    @Autowired
    private IMicrolessonService microlessonService;

    /**
     * 目录管理
     */
    @GetMapping("/SelectAll")
    public ApiReturnObject selectAll() {
        List<CatalogFree> freeList = catalogFreeService.selectByPatentId(0);
        for (CatalogFree free : freeList) {
            List<CatalogFree> list = catalogFreeService.selectByPatentId(free.getCatalogId());
            free.setChildren(list);
            for (CatalogFree catalogFree : list) {
                CatalogFree free1 = catalogFreeService.selectCatalogFreeById(catalogFree.getParentId().toString());
                catalogFree.setParentName(free1.getName());
            }
        }
        return new ApiReturnObject("200", "操作成功", freeList);
    }

    @GetMapping("/SelectAlls")
    public ApiReturnObject selectAlls(String catalogId) {
        CatalogFree catalogFree1 = catalogFreeService.selectCatalogFreeByCatalogId(catalogId);
        if (catalogFree1 == null){
             return new ApiReturnObject("300", "操作失败", "查无数据");
        }
        List<CatalogFree> list = catalogFreeService.selectByPatentId(catalogFree1.getCatalogId());
        catalogFree1.setChildren(list);
        for (CatalogFree catalogFree : list) {
            CatalogFree free1 = catalogFreeService.selectCatalogFreeById(catalogFree.getParentId().toString());
            catalogFree.setParentName(free1.getName());
        }
        return new ApiReturnObject("200", "操作成功", catalogFree1);
    }

    /**
     * 搜索
     */
    @GetMapping("/Search")
    public ApiReturnObject search(CatalogFree catalogFree) {
        List<CatalogFree> list = catalogFreeService.selectCatalogFreeList(catalogFree);
        return new ApiReturnObject("200", "操作成功", list);
    }

    /**
     * 添加
     */
    @PostMapping("/ins")
    public ApiReturnObject insert(@RequestBody CatalogFree catalogFree) {
        if (catalogFree.getParentId() == null) {
            catalogFree.setParentId(0);
        }
        catalogFree.setCreateTime(new Date());
        catalogFreeService.insertCatalogFree(catalogFree);
        return new ApiReturnObject("200", "操作成功", "添加成功");
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public ApiReturnObject update(@RequestBody CatalogFree catalogFree) {
//        if (isNext(catalogFree.getCatalogId())) {
//            throw new BusinessException(CommonErrorCode.E_200111);
//        }
        int i = catalogFreeService.updateCatalogFree(catalogFree);
        return new ApiReturnObject("200", "操作成功", "修改成功");
    }

    public boolean isNext(Integer catalogId) {
        List<Integer> list = selectCatalogId(catalogId);
        list.add(catalogId);
        for (Integer integer : list) {
            if (catalogId == integer) {
                return true;
            }
        }
        return false;
    }

    public List<Integer> selectCatalogId(Integer catalogId) {
        List<Integer> list = new ArrayList<>();
        List<CatalogFree> catalogFrees = catalogFreeService.selectByPatentId(catalogId);
        if (catalogFrees.size() > 0) {
            for (CatalogFree catalogFree : catalogFrees) {
                list.add(catalogFree.getCatalogId());
            }
        }
        return list;
    }

    /**
     * 删除
     */
    @GetMapping("/delete")
    public ApiReturnObject delete(Integer catalogId) {
        List<CatalogFree> freeList = catalogFreeService.selectByPatentId(catalogId);
        if (freeList.size() > 0) {
            return new ApiReturnObject("300", "操作成功", "该目录有下级目录无法删除");
        }
        Microlesson microlesson = new Microlesson();
        microlesson.setRCatalogid(catalogId.toString());
        List<Microlesson> microlessons = microlessonService.selectMicrolessonList(microlesson);
        if (microlessons.size() > 0){
             return new ApiReturnObject("300", "操作成功", "该目录下有资源 无法删除");
        }
        catalogFreeService.deleteCatalogFreeByCatalogId(catalogId);
        return new ApiReturnObject("200", "操作成功", "操作成功");
    }
}
